package com.hroze.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.RandomStringUtils;

/**
 * 文件工具类
 * @author ahoo
 */
public class FileUtil {
	public static int n = 0;
	/**
	 * 上一次时间值
	 */
	public static long time = 0;

	public static void main(String[] args) {
	}

	/**
	 * 写入文件
	 * @param file 文件路径
	 * @param size 文件大小的上限
	 * @param content 写入的内容
	 * @return 写入是否成功
	 */
	public static boolean write(String file, long size, String content) {
		file.replaceAll("/", File.separator);
		File f = new File(file);
		if (f.exists() && f.isFile()) {
			long l = f.length();
			long m = content.getBytes().length;
			if (l + m > size) {
				File newFile = new File(getNewName(file));
				if (f.renameTo(newFile)) {
					try {
						File a = new File(file);
						if (a.createNewFile()) {
							write(a, content);
						} else {
							return false;
						}
					} catch (IOException e) {
						e.printStackTrace();
						return false;
					}
				}

			} else {
				write(f, content);
			}
		} else {
			try {
				if (f.createNewFile()) {
					write(f, content);
				}
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	/**
	 * 20位无重复id（用于文件名）
	 * 
	 * @return
	 */
	public static String getName() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date d = new Date();
		long t = d.getTime();
		if (t <= time) {
			t = time + 1;
		}
		time = t;
		d = new Date(t);
		String dateStr = sdf.format(d);
		return dateStr + RandomStringUtils.randomNumeric(3);
	}

	/**
	 * 生成新文件名
	 * @param name
	 * @return
	 */
	public static String getNewName(String name) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String dateStr = sdf.format(new Date());/* +Double.valueOf((Math.random()*1000)).intValue() */
		String newName = "";
		if (name.indexOf(".") != -1) {
			String ext = name.substring(name.lastIndexOf("."));
			newName = name.substring(0, name.lastIndexOf("."));
			newName = newName + "-" + dateStr + ext;
		} else {
			newName = name + "-" + dateStr;
		}
		System.out.println(newName);
		return newName;
	}

	/**
	 * 像文件中写入内容
	 * @param file
	 * @param content
	 */
	public static void write(File file, String content) {
		try {
			FileWriter fw = new FileWriter(file, true);
			PrintWriter pw = new PrintWriter(fw);
			pw.println(content);
			pw.close();
			fw.close();
		} catch (IOException ex) {
			// System.out.println("异常" + ex.toString());
		}
	}

	/**
	 * 使用文件路径删除文件
	 * @param filePath
	 */
	public static void delete(String filePath) {
		File file = new File(filePath);
		delete(file);
	};

	/**
	 * 删除文件
	 * @param file
	 */
	public static void delete(File file) {
		if (file.exists()) {
			file.delete();
		}
	};
}
